<?php

declare(strict_types=1);

namespace App\Controller\MchApi;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;

abstract class MchBaseController
{
    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;

    /**
     * 表单校验抛异常封装
     *
     * @param       $params
     * @param array $rules
     * @param array $messages
     * @param array $customAttrs
     */
    protected function _validOrFail ($params, $rules = [], $messages = [], $customAttrs = []) {

        $validator = $this->validationFactory->make($params, $rules, $messages, $customAttrs);

        if ($validator->fails()) {
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }
    }
}
